package com.brillilab.starter.controller.aio.lab;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.lab.Organization;
import com.brillilab.service.core.lab.IOrganizationService;
import com.brillilab.starter.aspect.AspectLog;

/**
 * 组织管理API
 */
@RestController
@RequestMapping("/aio/organization")
public class OrganizationController {

    @Autowired
    private IOrganizationService organizationService;

    /**
     * 新增组织
     * @param organization
     * @return
     */
    @AspectLog
    @PostMapping("/add")
    public ResponseVo organizationAdd(@RequestBody Organization organization){

        if(organization == null || StringUtils.isEmpty(organization.getName()) || organization.getParentId() == null){
            return ResponseVo.failure(ResultEnum.REQUEST_PARAM_LACK,"组织名称缺失！",null);
        }

        Organization resultVo = organizationService.insert(organization);
        return ResponseVo.success(resultVo);
    }

    /**
     * 通过父级ID获取组织列表
     * {\"parentId\":\"父级ID 必填顶级地区填0 L\"}
     * @return
     */
    @GetMapping("/list")
    public ResponseVo organizationList(Long parentId){
        if(parentId == null){
            return ResponseVo.failure(ResultEnum.REQUEST_PARAM_LACK);
        }

       List<Organization> resultVo = organizationService.orgList(parentId);
       return ResponseVo.success(resultVo);
    }

    /**
     * 组织分页
     * {\"pageNum\":\"跳转到的页数 L\",\"pageSize\":\"每页记录条数 L\"}
     * @return
     */
    @PostMapping("/page")
    public ResponseVo organizationPage(@RequestBody JSONObject json){

    	int pageNum = json.getInteger("pageNum") != null ?json.getInteger("pageNum") :1;
    	int pageSize = json.getInteger("pageSize") != null ?json.getInteger("pageSize") :10;

        PageVo<Organization> resultVo = organizationService.queryPage(null,pageNum,pageSize);
        return ResponseVo.success(resultVo);
    }

}
